package com.wcs.spring.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.wcs.spring.entity.ClassTable;
import com.wcs.spring.entity.OrderClass;

/**
 * <p>
 * Title: Phicomm_[子系统统名]_[模块名]
 * </p>
 * <p>
 * Description: [描述该类概要功能介绍]
 * </p>
 * 
 * @author hexuan2014
 * @version $Revision$ 2015-6-23
 * @author (lastest modification by $Author$)
 * @since 1.0
 */
@Repository(value = "classTableMapper")
public interface ClassTableMapper {

	@Select(value = "${sql}")
	List<ClassTable> queryClassTableList(@Param(value = "sql") String sql);

	@Select(value = "${sql}")
	List<OrderClass> queryOrderClassList(@Param(value = "sql") String sql);

	@Insert("insert into classTable (classInfoId,levelType,classLevel,classAddr,classTeacherId,schoolId,studentNum,classDate,classStartTime,classEndTime,createTime,updateTime,createUser,updateUser,flag) "
			+ "values (#{classInfoId},#{levelType},#{classLevel},#{classAddr},#{classTeacherId},#{schoolId},#{studentNum},#{classDate},#{classStartTime},#{classEndTime},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
	@SelectKey(statement = "SELECT LAST_INSERT_ID() AS id", keyProperty = "id", before = false, resultType = Integer.class)
	int addClassTable(ClassTable classTable);

	@Update("UPDATE classTable SET classInfoId=#{u.classInfoId}, levelType=#{u.levelType},classAddr=#{u.classAddr} "
			+ "classLevel=#{u.classLevel}, classTeacherId=#{u.classTeacherId},studentNum=#{u.studentNum},"
			+ " updateTime=#{u.updateTime}, updateUser=#{u.updateUser} " + " WHERE id=#{u.id}")
	void updateClassTable(@Param("u") ClassTable classTable);

	@Update("UPDATE classTable SET flag=1 WHERE id=#{u.id}")
	void delClassTable(@Param("u") ClassTable classTable);

	@Delete("DELETE FROM classTable WHERE id = #{u.id}")
	void deleteClassTable(@Param("u") ClassTable classTable);

	@Select({ "SELECT ct.*,(SELECT COUNT(*) FROM orderclass oc where oc.classTableId=ct.id) orderClassNum, "
			+ "u.userName classTeacherName,ci.className classInfoName "
			+ "from classtable ct ,classinfo ci ,user u where ct.classInfoId=ci.id and ct.classTeacherId=u.id  and ct.id= #{id}" })
	ClassTable queryClassTableById(Integer id);

	@Insert("insert into orderClass (studentId,classTableId,status,schoolId,createTime,updateTime,createUser,updateUser,flag) "
			+ "values (#{studentId},#{classTableId},0,#{schoolId},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
	@SelectKey(statement = "SELECT LAST_INSERT_ID() AS id", keyProperty = "id", before = false, resultType = Integer.class)
	int addOrderClass(OrderClass orderClass);

	@Update("UPDATE orderClass SET status=#{u.status}, updateTime=#{u.updateTime}, updateUser=#{u.updateUser} "
			+ " WHERE studentId=#{u.studentId} and classTableId = #{u.classTableId}")
	void updateOrderClass(@Param("u") OrderClass orderClass);
}
